package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.jd.JdAccidentModelSectionData;
import com.ucredit.babel.common.model.third.jd.JdAccidentModelSectionRecord;
import com.ucredit.babel.common.model.third.jd.JdReimbursementAbilityData;
import com.ucredit.babel.common.model.third.jd.JdReimbursementAbilityRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Classname JingDongReimbursementAbilityParse
 * @Description 京东-意外险风险评分 20210423-20210608
 * @Date 2021/6/9 20:51
 * @Created by lzw
 */
public class JingDongAccidentModelSectionParse extends ParseBase {
    Text key = new Text();
    Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        JdAccidentModelSectionRecord obj = (JdAccidentModelSectionRecord) model;
        JdAccidentModelSectionData info = obj.getData();

        StringBuilder sb = getBaseInfo(obj, successful);

//        sb.append(info.getCode()).append(CRT001);
//        sb.append(info.getMsg()).append(CRT001);

        String string = info.getData();

        JSONObject data1 = JSON.parseObject(string) == null ? new JSONObject() : JSON.parseObject(string);
        JSONObject data2 = data1.getJSONObject("data") == null ? new JSONObject() : data1.getJSONObject("data");

        sb.append(data1.getString("code") == null ? "" : data1.getString("code")).append(CRT001);
        sb.append(data1.getString("msg") == null ? "" : data1.getString("msg")).append(CRT001);

        sb.append(data2.getString("refuse_flag") == null ? "" : data2.getString("refuse_flag")).append(CRT001);
        sb.append(data2.getString("hit_type") == null ? "" : data2.getString("hit_type")).append(CRT001);
        sb.append(data2.getString("pa_model_scr") == null ? "" : data2.getString("pa_model_scr")).append(CRT001);

        key.set(getKey());
        value.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, value);

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_jingdong_accident_model_section_inc";
    }

    @Override
    public String getType() {
        return RecordType.JD_ACCIDENT_MODEL_SECTION_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "京东-意外险风险评分";
    }
}
